package common.future;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * Created by dong peng on 2018/8/19.
 *
 * 测试Future获取子线结果
 */
public class FutureTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService executorService = Executors.newCachedThreadPool();
        ChildTask childTask = new ChildTask();
        Future<Map> result = executorService.submit(childTask);
        childTask = new ChildTask();
        Future<Map> result2 = executorService.submit(childTask);
        executorService.shutdown();

        Thread.sleep(1000);
        System.out.println("主线执行其他逻辑完成");

        Map map = result.get();
        System.out.println("子线返回的结果: " + map);
        Map map2 = result2.get();
        System.out.println("子线返回的结果: " + map2);

        System.out.println("==========================");
    }


}


class ChildTask implements Callable<Map> {

    @Override
    public Map call() throws Exception {

        Map map = new HashMap();
        map.put("name", Thread.currentThread().getName());

        Thread.sleep(1000);

        return map;
    }
}
